
set more off

clear all
set more off

**Create stacked IO_table
clear

insheet using "$inputs/IO_tables/absorb_simple.csv"

forvalues i=1(1)130 {
	local j = `i' + 1
	rename v`j' IO_`i'
}

rename io_code io_input

reshape long IO_, i(io_input) j(io_output)
rename IO_ input_expenditure
drop if input_expenditure==.
rename io_output io
sort io io_input
save "$working/IO_table_stack", replace

***Step 1: For each NIC, generate total input expenditures for each IO industry; this sums up expenditures from multiple matches. Then generate weights.

clear

insheet using "$inputs/handcoded/NIC_IO_Crosswalk.csv"

drop description

reshape long io_, i(digitnic2008) j(count)

rename io_ io

drop if io==.
drop count

sort io digitnic2008

levelsof digitnic2008, local(nicvals)


foreach nic of local nicvals {
	preserve
		display("`val'")
		
		keep if digitnic2008==`nic'

		merge 1:m io using "$working/IO_table_stack"

		keep if _merge==3
		drop _merge

		collapse (sum) input_expenditure, by(io_input digitnic2008)

		egen total_input_expenditure = total(input_expenditure)
		generate input_weight = input_expenditure/total_input_expenditure

		sort input_weight

		keep digitnic2008 io_input input_weight 

		save "$working/temp_`nic'", replace
	restore
}

clear

foreach nic of local nicvals {
	append using "$working/temp_`nic'"
}

save "$working/NIC_to_IO_weights", replace

clear

foreach nic of local nicvals {
	erase "$working/temp_`nic'.dta"
}

clear

use "$clean/trade_data.dta"

destring ISIC4, replace

tempfile mtrade

save `mtrade', replace

**Generate m:m crosswalk (will loop to make sure merges work correctly)
clear

insheet using "$inputs/handcoded/NIC_IO_Crosswalk.csv"

drop description

drop if io_1==. //No matches
isid digitnic2008

rename digitnic2008 ISIC4

reshape long io_, i(ISIC4) j(count)

rename io_ io

drop if io==.
drop count

sort io

save "$working/NIC_IO_m_m_crosswalk", replace

levelsof io, local(iovals)

local ioval = 1
*Grabbing one io value at a time, so merges work; sum up all trade values that match to a given ISIC4 for each year
foreach ioval of local iovals {
	preserve
		keep if io==`ioval'
		
		isid ISIC4
		
		merge 1:m ISIC4 using `mtrade'
		keep if _merge==3
		drop _merge
		
		collapse (sum) IND_CHN IND_WLD LMI_CHN LMI_WLD LMI_WLD_EXCL_IND LMI_CHN_EXCL_IND, by(io year)
		
		save "$working/temp_`ioval'", replace
	restore
}

clear
foreach ioval of local iovals {
	append using "$working/temp_`ioval'"
}

save "$working/IO_trade_year", replace

clear

foreach ioval of local iovals {
	erase "$working/temp_`ioval'.dta"
}

clear

use "$working/NIC_to_IO_weights"

rename digitnic2008 ISIC4 

**Make dataset ISIC4 IO year: then merge with the IO year trade data, above
expand 32

bysort ISIC4 io: generate mid_year = _n
generate year=.
forvalues i = 1(1)32 {
	replace year = 1988 + `i' if mid_year==`i'
}

drop mid_year

rename io_input io

merge m:1 io year using "$working/IO_trade_year"
drop if _merge==1 // no trade data
drop _merge

bysort ISIC4 year: egen rescale_iw = total(input_weight)

sum rescale_iw, detail

generate rs_input_weight = input_weight/rescale_iw

foreach value in IND_CHN IND_WLD LMI_CHN LMI_WLD LMI_WLD_EXCL_IND LMI_CHN_EXCL_IND {
	generate wIPT_`value' = input_weight*`value'
	generate rwIPT_`value' = rs_input_weight*`value'
}

collapse (sum) wIPT_* rwIPT_*, by(ISIC4 year)

rename ISIC4 nic_08_4dig
tostring(nic_08_4dig), replace
save "$working/IPT_TRADE_DATA", replace

merge 1:m nic_08_4dig year using "$clean/prowess_wits.dta"
keep if _merge==3
drop _merge

***We will not use this:

drop sa_total_assets sa_gross_fixed_asset_2206 sa_intangible_ast sa_net_goodwill sa_goodwill sa_goodwill_addn sa_goodwill_addn_rev_2211 sa_goodwill_deduct ///
sa_goodwill_cumm_dep_2213 sa_goodwill_dep sa_net_sw sa_software sa_sw_addn sa_sw_addn_reval sa_sw_deduct sa_sw_cumm_dep sa_sw_dep sa_net_mining_rights_2222 sa_mining_rights_ass_2223 ///
 sa_mining_rights_add_2224 sa_mining_rights_add_2225 sa_mining_rights_del_2226 sa_mining_rights_cum_2227 sa_mining_rights_dep_2228 sa_net_oth_intangibl_2229 sa_oth_intangible_as_2230 ///
 sa_oth_intangible_as_2231 sa_oth_intangible_as_2232 sa_oth_intangible_as_2233 sa_oth_intangible_as_2234 sa_oth_intangible_as_2235 sa_intangible_ast_ad_2236 sa_intangible_ast_ad_2237 ///
 sa_intangible_ast_de_2238 sa_intangible_ast_cu_2239 sa_intangible_ast_de_2240 sa_net_intangible_as_2241 sa_land_n_building sa_net_land sa_land sa_land_addn sa_land_addn_reval sa_land_deduct ///
 sa_land_cumm_dep sa_land_dep sa_net_freehold_land_2250 sa_net_leasehold_lan_2251 sa_net_leasehold_imp_2252 sa_gross_leasehold_i_2253 sa_leasehold_imprvmn_2254 sa_leasehold_imprvmn_2255 ///
 sa_leasehold_imprvmn_2256 sa_leasehold_imprvmn_2257 sa_leasehold_imprvmn_2258 sa_net_building sa_building sa_building_addn sa_building_addn_rev_2262 sa_building_deduct ///
 sa_building_cumm_dep_2264 sa_building_dep sa_land_building_add_2266 sa_land_n_building_a_2267 sa_land_n_building_d_2268 sa_land_n_building_c_2269 sa_land_n_building_d_2270 ///
 sa_net_land_n_buildi_2271 sa_plant_mach_comput_2272 sa_net_plant sa_plant sa_plant_addn sa_plant_addn_reval sa_plant_deduct sa_plant_cumm_dep sa_plant_dep sa_net_computer_it ///
 sa_computer_it sa_computer_it_addn sa_computer_it_addn__2283 sa_computer_it_deduc_2284 sa_computer_it_cumm__2285 sa_computer_it_dep sa_net_elec_install__2287 sa_elec_install_fitt_2288 ///
 sa_elec_install_fitt_2289 sa_elec_install_fitt_2290 sa_elec_install_fitt_2291 sa_elec_install_fitt_2292 sa_elec_install_fitt_2293 sa_plant_mach_comput_2294 v295 sa_plant_mach_comput_2296 ///
 sa_plant_mach_comput_2297 sa_plant_mach_comput_2298 sa_net_plant_mach_co_2299 sa_transport_comm_eq_2300 sa_net_transport_inf_2301 sa_transport_infra sa_transport_infra_a_2303 ///
 sa_transport_infra_a_2304 sa_transport_infra_d_2305 sa_transport_infra_c_2306 sa_transport_infra_d_2307 sa_net_transport_veh_2308 sa_transport_vehicle_2309 sa_transport_vehicle_2310 ///
 sa_transport_vehicle_2311 sa_transport_vehicle_2312 sa_transport_vehicle_2313 sa_transport_vehicle_2314 sa_net_comm_equip sa_comm_equip sa_comm_equip_addn sa_comm_equip_addn_r_2318 ///
 sa_comm_equip_deduct_2319 sa_comm_equip_cumm_d_2320 sa_comm_equip_dep sa_transport_comm_eq_2322 v323 sa_transport_comm_eq_2324 sa_transport_comm_eq_2325 v326 sa_net_transport_com_2327 ///
 sa_furn_social_oth_f_2328 sa_net_furn_and_fixt_2329 sa_furn_and_fixtures_2330 sa_furn_and_fixtures_2331 sa_furn_and_fixtures_2332 sa_furn_and_fixtures_2333 sa_furn_and_fixtures_2334 ///
 sa_furn_and_fixtures_2335 sa_net_social_amenit_2336 sa_social_amenities sa_social_amenities__2338 sa_social_amenities__2339 sa_social_amenities__2340 sa_social_amenities__2341 ///
 sa_social_amenities__2342 sa_net_oth_fixed_ast_2343 sa_oth_fixed_ast sa_oth_fixed_ast_add_2345 sa_oth_fixed_ast_add_2346 sa_oth_fixed_ast_ded_2347 sa_oth_fixed_ast_cum_2348 ///
 sa_oth_fixed_ast_dep_2349 sa_furn_social_oth_f_2350 v351 sa_furn_social_oth_f_2352 sa_furn_social_oth_f_2353 sa_furn_social_oth_f_2354 sa_net_furn_social_o_2355 ///
 sa_gross_fixed_ast_a_2356 sa_gross_fixed_ast_a_2357 sa_gross_fixed_ast_d_2358 sa_gross_fixed_ast_c_2359 sa_gross_fixed_ast_d_2360 sa_net_fixed_assets 

save "$clean/prowess_wits_IO.dta", replace
